home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / netprog.zip / NETPROG.TAR / lock / lockopen.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  731b  |  39 lines

  1. /*
  2.  * Locking routines using a open() system call with both
  3.  * O_CREAT and O_EXCL specified.
  4.  */
  5.  
  6. #include    <fcntl.h>
  7. #include    <sys/errno.h>
  8. extern int    errno;
  9.  
  10. #define    LOCKFILE    "seqno.lock"
  11. #define    PERMS        0666
  12.  
  13. my_lock(fd)
  14. int    fd;
  15. {
  16.     int    tempfd;
  17.  
  18.     /*
  19.      * Try to create the lock file, using open() with both
  20.      * O_CREAT (create file if it doesn't exist) and O_EXCL
  21.      * (error if create and file already exists).
  22.      * If this fails, some other process has the lock.
  23.      */
  24.  
  25.     while ( (tempfd = open(LOCKFILE, O_RDWR|O_CREAT|O_EXCL, PERMS)) < 0) {
  26.         if (errno != EEXIST)
  27.             err_sys("open error for lock file");
  28.         sleep(1);
  29.     }
  30.     close(tempfd);
  31. }
  32.  
  33. my_unlock(fd)
  34. int    fd;
  35. {
  36.     if (unlink(LOCKFILE) < 0)
  37.         err_sys("unlink error for lock file");
  38. }
  39.